import {createAsyncThunk} from "@reduxjs/toolkit";
import {PagingData, PagingResult} from "type/Page";
import {SecurityRole} from "type/entity/SecurityRole";
import RequestHandler from "http/RequestHandler";

/** 请求角色的分页数据 */
export const fetchRolePageData = createAsyncThunk(
    'role/fetchRolePageData',
    async (paging: PagingData) => {
        return await RequestHandler.get<PagingResult<SecurityRole>>("/manager/admin/role/page/" + paging.page + "/" + paging.pageSize);
    }
);

/** 请求服务器添加角色数据 */
export const fetchAddRole = createAsyncThunk(
    'role/fetchAddRole',
    async (role: SecurityRole) => {
        return await RequestHandler.put<number>("/manager/admin/role", role);
    }
);

/** 请求服务器更新角色数据 */
export const fetchUpdateRole = createAsyncThunk(
    'role/fetchUpdateRole',
    async (role: SecurityRole) => {
        return await RequestHandler.post<number>("/manager/security-role", role);
    }
);

/**
 * 请求服务器删除角色数据
 * @param roleId 角色ID
 *  */
export const fetchDeleterRole = createAsyncThunk(
    'role/fetchDeleteRole',
    async (roleId: SecurityRole) => {
        return await RequestHandler.delete<number>("/manager/security-role/" + roleId);
    }
);